package main

import "fmt"

/*
 You'll need to use select statements,goroutines and channels to returns
the sum of all squares between 1 and c,for example:
1*1 + 2*2 + 3*3 + 4*4 + 5*5 = 55
*/

func main() {

	sumChan := make(chan int)
	quitChan := make(chan int)

	sum := 0

	//用于求和的goroutine
	go func() {
		for i := 1; i <= 5; i++ {
			sum += <-sumChan
		}
		fmt.Println("sum=", sum)

		//通知结束
		quitChan <- 0
	}()

	//用于求平方

	SumOfSquares(sumChan, quitChan)

}

func SumOfSquares(sumChan, quitChan chan int) {
	num := 1
	for {
		select {
		case sumChan <- num * num:
			num++
		case <-quitChan:
			return
		}
	}
}

//OutPut: sum= 55
